package com.example.transactionhintassistant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.transactionhintassistant.pojo.dto.ringtone.AddRingtoneDto;
import com.example.transactionhintassistant.pojo.dto.ringtone.ChangeRingtoneDto;
import com.example.transactionhintassistant.pojo.entity.Ringtone;
import com.example.transactionhintassistant.pojo.vo.RingtoneVo;
import com.example.transactionhintassistant.service.RingtonesService;
import com.example.transactionhintassistant.mapper.RingtonesMapper;
import com.example.transactionhintassistant.utils.result.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 86147
 * @description 针对表【ringtones(铃声表)】的数据库操作Service实现
 * @createDate 2024-03-04 22:32:13
 */
@Service
public class RingtonesServiceImpl extends ServiceImpl<RingtonesMapper, Ringtone>
        implements RingtonesService {

    @Autowired
    private RingtonesMapper ringtonesMapper;

    @Override
    public Result<List<RingtoneVo>> getAllRingtones() {
        List<RingtoneVo> ringtoneVos = ringtonesMapper.getAllRingtones();
        return Result.ok(ringtoneVos);
    }

    @Override
    public Result<Object> addRingtone(AddRingtoneDto addRingtoneDto) {
        Ringtone ringtone = new Ringtone();
        BeanUtils.copyProperties(addRingtoneDto, ringtone);
        this.save(ringtone);
        return Result.ok(null, "添加成功！");
    }

    @Override
    public Result<Object> changeRingtone(ChangeRingtoneDto changeRingtoneDto) {
        if (this.isRingtoneExists(changeRingtoneDto.getId())) {
            Ringtone ringtone = new Ringtone();
            BeanUtils.copyProperties(changeRingtoneDto, ringtone);
            this.updateById(ringtone);
            return Result.ok(null, "修改成功！");
        }
        return Result.build(null, 500, "铃声不存在");
    }


    @Override
    public Result<Object> deleteRingtone(Integer id) {
        if (this.isRingtoneExists(id)) {
            this.removeById(id);
            return Result.ok(null, "删除成功！");
        }
        return Result.build(null, 500, "铃声不存在");
    }

    @Override
    public boolean isRingtoneExists(Integer ringtoneId) {
        LambdaQueryWrapper<Ringtone> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Ringtone::getId, ringtoneId);
        return this.exists(lambdaQueryWrapper);
    }
}




